<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class TopicLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $topicModel;

    public function _initialize()
    {
        $this->topicModel = D('Topic', 'Model');
    }


    public function getTopicList($page = 1, $size = 10, $params)
    {
        $map = [];
        if ($params['t_id']) {
            $map['topic_id'] = ['EQ', $params['t_id']];
        }
        if ($params['t_name']) {
            $map['topic_name'] = ['LIKE', "%" . $params['t_name'] . "%"];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['EQT', strtotime($params['start_time'])];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['ELT', strtotime($params['end_time'])];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = ['BETWEEN', [strtotime($params['start_time']), strtotime($params['end_time'])]];
        }
        $cols = 'topic_id,topic_name,create_time';
        $order = 'topic_id DESC';
        $total = $this->topicModel->where($map)->count();
        $list = $this->topicModel->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order($order)
            ->select();
        $listInfo = [];
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $listInfo[$k]['topic_id'] = $v['topic_id'];
                $listInfo[$k]['topic_name'] = $v['topic_name'];
                $listInfo[$k]['topic_num'] = D('Question', 'Logic')->getTopicNum($v['topic_id']);
                $listInfo[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
            }
        }
        $result = [
            'list'  => $listInfo,
            'total' => $total
        ];
        return $result;
    }

    public function getTopicById($topicID)
    {
        if ($topicID < 1) {
            return false;
        }
        $condition = [];
        $condition['topic_id'] = ['EQ', $topicID];
        return $this->topicModel->field('*')->where($condition)->find();
    }


    public function addTopic($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['status'] = 1;
        $data['create_time'] = NOW_TIME;
        $data['update_time'] = NOW_TIME;

        return $this->topicModel->add($data);
    }
}